package org.yanfengying.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yanfengying.dto.BinderDTO;
import org.yanfengying.dto.PasswordDTO;
import org.yanfengying.dto.PhoneLoginDTO;
import org.yanfengying.dto.RegisterSmsDTO;
import org.yanfengying.user.service.IVerifyCodeService;
import org.yanfengying.utils.AjaxResult;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/register")
    public AjaxResult sendRegisterSmsCode(@RequestBody RegisterSmsDTO dto){
        try {
            verifyCodeService.sendRegisterSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }

    @GetMapping("/image/{key}")
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            String imageCode = verifyCodeService.getImageCode(key);
            return AjaxResult.me().setResult(imageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取图形验证码失败"+e.getMessage());
        }
    }
    /**
     * 获取短信验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/binder")
    public AjaxResult sendBinderSmsCode(@RequestBody BinderDTO dto){
        try {
            verifyCodeService.sendBinderSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }

    /**
     * 验证码登录获取验证码
     * @param dto
     * @return
     */
    @PostMapping("/sms/phone")
    public AjaxResult sendPhoneSmsCode(@RequestBody PhoneLoginDTO dto){
        try {
            verifyCodeService.sendPhoneSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }
    @PostMapping("/sms/password")
    public AjaxResult sendPasswordSmsCode(@RequestBody PasswordDTO dto){
        try {
            verifyCodeService.sendPasswordSmsCode(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("短信验证码发送失败!"+e.getMessage());
        }
    }
}
